const util = require('../../../utils/util.js')
const request = require('../../../utils/request.js')
const app = getApp()
Page({
  data: {
    // anonymousBuyer: false,
    /**总价 */
    totalPrice: -1,
    orderItemsArr: [],
    defaultArr: [],
    addressId: -1
  },
  onLoad: function(options) {
    var that = this;
    var orderItemsArr = app.globalData.orderItemsArr;
    var totalPrice = 0;
    orderItemsArr, totalPrice = that.updateNum(orderItemsArr, totalPrice);
    that.setData({
      orderItemsArr: orderItemsArr,
      totalPrice: totalPrice
    })
  },

  updateNum: function(orderItemsArr, totalPrice) {
    for (var i = 0; i < orderItemsArr.length; i++) {
      var storeTotaltTransCost = 0;
      var storeTotalPrice = 0;
      var storeTotalItems = 0;
      var goodsArr = orderItemsArr[i].goodsArr;
      for (var j = 0; j < goodsArr.length; j++) {
        storeTotaltTransCost += goodsArr[j].transCost;
        storeTotalPrice += (parseInt(goodsArr[j].goodPrice)) * (parseInt(goodsArr[j].goodCnt));
        storeTotalItems += parseInt(goodsArr[j].goodCnt);
      }
      orderItemsArr[i].storeTotaltTransCost = storeTotaltTransCost;
      orderItemsArr[i].storeTotalItems = storeTotalItems;
      orderItemsArr[i].storeTotalPrice = storeTotalPrice;
      totalPrice += storeTotaltTransCost + storeTotalPrice;
    }
    return orderItemsArr, totalPrice;
  },
  onShow: function(e) {
    //请求获取 地址列表
    var that = this;
    var chooseAddr = that.data.chooseAddr;
    var url = request.URL + "/user/address";
    var defaultAddr = [];
    request.request({
      url: url,
      data: {
        userId: 1
      },
      success: function(res) {
        var address_list = res.data;
        var addressId = that.data.addressId;
        console.log(addressId);
        if (addressId != -1) {
          var selectedAddressId = addressId;
          for (var i = 0; i < address_list.length; i++) {
            if (selectedAddressId == address_list[i].addressId) {
              defaultAddr = address_list[i];
            }
          }
        } else {
          defaultAddr = address_list[0];
          addressId = address_list[0].addressId;
        }
        that.setData({
          defaultAddr: defaultAddr,
          addressId: addressId
        })
        console.log(that.data.defaultAddr);
      },
      fail: function() {
        request.showFailInfo()
      }
    })
  },
  /**点击进入地址列表 */
  toAddressList: function(e) {
    wx.navigateTo({
      url: '/pages/address_list/choose_address/choose_address',
    })
  },
  // /**点击添加物品数量 */
  // add_item_num: function(e) {
  //   var that = this;
  //   var selectedGoodId = e.target.dataset.goodid;
  //   var selectedStandard = e.target.dataset.standard;
  //   var orderItemsArr = that.data.orderItemsArr;
  //   var totalPrice = 0;
  //   for (var i = 0; i < orderItemsArr.length; i++) {
  //     var goodsArr = orderItemsArr[i].goodsArr;
  //     for (var j = 0; j < goodsArr.length; j++) {
  //       var goodId = goodsArr[j].goodId;
  //       var standard = goodsArr[j].standard;
  //       if (selectedGoodId == goodId && selectedStandard == standard) {
  //         var goodCnt = parseInt(goodsArr[j].goodCnt);
  //         goodCnt += 1;
  //         goodsArr[j].goodCnt = goodCnt;
  //       }
  //     }
  //   }
  //   orderItemsArr, totalPrice = that.updateNum(orderItemsArr, totalPrice);
  //   that.setData({
  //     orderItemsArr: orderItemsArr,
  //     totalPrice: totalPrice
  //   })

  // },
  // /**点击减少商品 */
  // minus_item_num: function(e) {
  //   var that = this;
  //   var selectedGoodId = e.target.dataset.goodid;
  //   var selectedStandard = e.target.dataset.standard;
  //   var orderItemsArr = that.data.orderItemsArr;
  //   var totalPrice = 0;
  //   for (var i = 0; i < orderItemsArr.length; i++) {
  //     var goodsArr = orderItemsArr[i].goodsArr;
  //     for (var j = 0; j < goodsArr.length; j++) {
  //       var goodId = goodsArr[j].goodId;
  //       var standard = goodsArr[j].standard;
  //       if (selectedGoodId == goodId && selectedStandard == standard) {
  //         if (goodsArr[j].goodCnt > 1) {
  //           goodsArr[j].goodCnt -= 1;
  //         } else {
  //           goodsArr[j].goodCnt = 1;
  //         }
  //       }
  //     }
  //   }
  //   orderItemsArr, totalPrice = that.updateNum(orderItemsArr, totalPrice);
  //   that.setData({
  //     orderItemsArr: orderItemsArr,
  //     totalPrice: totalPrice
  //   })
  // },
  // /**输入数量 */
  // updateItemNum: function(e) {
  //   var that = this;
  //   var selectedGoodId = e.target.dataset.goodid;
  //   var selectedStandard = e.target.dataset.standard;
  //   var orderItemsArr = that.data.orderItemsArr;
  //   var totalPrice = 0;
  //   var value = e.detail.value;
  //   for (var i = 0; i < orderItemsArr.length; i++) {
  //     var goodsArr = orderItemsArr[i].goodsArr;
  //     for (var j = 0; j < goodsArr.length; j++) {
  //       var goodId = goodsArr[j].goodId;
  //       var standard = goodsArr[j].standard;
  //       if (selectedGoodId == goodId && selectedStandard == standard) {
  //         goodsArr[j].goodCnt = value;
  //       }
  //     }
  //   }
  //   orderItemsArr, totalPrice = that.updateNum(orderItemsArr, totalPrice);
  //   console.log(that.data.orderItemsArr)
  //   that.setData({
  //     orderItemsArr: orderItemsArr,
  //     totalPrice: totalPrice
  //   })
  // },
  /**修改是否匿名购买 */
  // changeAnonyStatus: function(e) {
  //   var that = this;
  //   var value = e.detail.value;
  //   console.log(value);
  //   that.setData({
  //     anonymousBuyer: value
  //   })
  // },
  /**创建订单 */
  createOrder: function() {

    var that = this;
    var orderItemsArr = [];
    var addressId = that.data.addressId;
    console.log(addressId)
    orderItemsArr.orderStatus = 1;
    orderItemsArr.orderItemsArr = that.data.orderItemsArr;
    orderItemsArr.orderItemsArr.addressId = addressId;
    app.globalData.orderItemsArr = orderItemsArr;
    console.log(app.globalData.orderItemsArr);
    wx.navigateBack({
      delta: 1
    })
    wx.navigateTo({
      url: '/pages/order_list/order_details/order_details',
    })
  },
  /**找朋友代付 */
  toSharePage: function(e) {

  }
})